//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ViNoToolkit;

/// <summary>
/// To save an object information , this component is programmatically addded to an object.
/// </summary>
public class ViNoSceneNode : ViNode {
	public bool activeAtStart = true;
	public string currentLoadedTexturePath;	

	public SceneData.SceneNodeData	singleNodeData = new SceneData.SceneNodeData();	
	
	[HideInInspector ] public bool isCacheObject;
	[HideInInspector ] public bool _RELOAD;	

	public bool makePixelPerfect{ set; get; }

	static public Hashtable MakeHash( SceneData.SceneNodeData data ){
		Hashtable param = new Hashtable();
		param[ "name" ]				= data.name;	
		param[ "parentname" ]		= data.parentname;
		param[ "posX" ]				= data.posX.ToString();
		param[ "posY" ]				= data.posY.ToString();
		param[ "posZ" ]				= data.posZ.ToString();
		param[ "rotX" ]				= data.rotX.ToString();
		param[ "rotY" ]				= data.rotY.ToString();
		param[ "rotZ" ]				= data.rotZ.ToString();
		param[ "sclX" ]				= data.sclX.ToString();
		param[ "sclY" ]				= data.sclY.ToString();
		param[ "sclZ" ]				= data.sclZ.ToString();
		param[ "uiAtlasName" ]		= data.uiAtlasName;
		param[ "texturePath" ]		= data.texturePath;
		param[ "alpha" ]			= data.alpha.ToString();		
		param[ "show" ]				= data.show ? "true" : "false";	
		param[ "makePixelPerfect" ]	= data.makePixelPerfect ? "true" : "false";	
		return param;
	} 

	public override void ToScenarioScript( ref System.Text.StringBuilder sb ){
		//TODO : IMPL.
	
	}

	public override void ToByteCode( ByteCodes code ){
		List<byte> byteList = new List<byte>();
		AddNodeCodeWithTag( byteList , name );

		Hashtable hash = MakeHash( singleNodeData );
		ByteCodeScriptTools.AddTablePairsCode( byteList , hash );
		byteList.Add( Opcode.SCENE_NODE );
		code.Add( byteList.ToArray() );

		ToByteCodeInternal( code );
	}

	public void Reload(){
		SceneData.SceneNodeData data = singleNodeData;
		if( data == null ){
			ViNoDebugger.LogWarning( "SingleNodeData not attached." );
			return;
		}		
		GameObject parent = GameObject.Find( data.parentname );		
		
		Vector3 pos = new Vector3( data.posX , data.posY , data.posZ );
		Vector3 rot = new Vector3( data.rotX , data.rotY , data.rotZ );	
		Vector3 scl	= new Vector3( data.sclX , data.sclY , data.sclZ );
		
		ViNoGOExtensions.SetTransform( data.name , gameObject , parent , pos , rot , scl );
				
		ColorPanel panel = GetComponent<ColorPanel>();
		if( panel != null ){
			panel.alpha = data.alpha;		
		}		
	}


	void OnChangeTexture( string texPath ){
		currentLoadedTexturePath = texPath;
	}

	void Awake(){
		if( ! activeAtStart ){
			gameObject.SetActive( false );
		}

		if( isCacheObject ){
			GOCache.Add( this.gameObject );
		}
	}
	
	void Update(){
		if( _RELOAD ){
			_RELOAD = false;
			Reload();
		}
	}
		
}
